﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System;
using System.Xml.Linq;
using ExtremeML.Spreadsheet;
using ExtremeML.Spreadsheet.Address;

namespace ExtremeML.Sdk.Vml
{
	/// <summary>
	/// Models the Vml drawing element x:ClientData.
	/// </summary>
	internal class ClientData : VmlElement
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ClientData"/> class.
		/// </summary>
		/// <param name="element">An <see cref="XElement"/> to which this instance is bound.</param>
		/// <param name="worksheet">The <see cref="WorksheetWrapper"/> that contains the worksheet targeted by the associated comment.</param>
		public ClientData(
			XElement element,
			WorksheetWrapper worksheet)
			: base(element)
		{
			if (worksheet == null) throw new ArgumentNullException("worksheet");

			_anchor = new Anchor(Element.Element(Utility.X + "Anchor"), worksheet);
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ClientData"/> class.
		/// </summary>
		/// <param name="element">An <see cref="XElement"/> to which this instance is bound.</param>
		/// <param name="worksheet">The <see cref="WorksheetWrapper"/> that contains the worksheet targeted by the associated comment.</param>
		/// <param name="cell">A <see cref="GridReference"/> that specifies the location of the associated cell.</param>
		public ClientData(
			XElement element,
			WorksheetWrapper worksheet,
			GridReference cell)
			: base(element)
		{
			if (worksheet == null) throw new ArgumentNullException("worksheet");

			_anchor = new Anchor(Element.Element(Utility.X + "Anchor"), worksheet, cell);
		}

		private readonly Anchor _anchor;

		/// <summary>
		/// Sets the properties of this element and regenerates the underlying Xml.
		/// </summary>
		/// <param name="properties">The properties of the associated shape.</param>
		public void Configure(
			ShapeProperties properties)
		{
			if (properties == null) throw new ArgumentNullException("properties");

			if (properties.Cell.HasValue)
			{
				Element.Element(Utility.X + "Row").Value = properties.Cell.Value.RowIndex.ToString();
				Element.Element(Utility.X + "Column").Value = properties.Cell.Value.ColumnIndex.ToString();
			}

			_anchor.Configure(properties);
		}
	}
}